package com.example.recyclerview_deep.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.recyclerview_deep.R;
import com.example.recyclerview_deep.holder.NormalHolder;
import com.example.recyclerview_deep.holder.SectionHolder;
import com.example.recyclerview_deep.pojo.Goods;

import java.util.List;

public class TypeViewAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List<Goods> mData;
    public static final int ITEM_TYPE_NORMAL = 0;//普通类型
    public static final int ITEM_TYPE_SECTION = 1;//特殊类型

    public TypeViewAdapter(Context mContext, List<Goods> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        //根据不同类型来获取不同的ViewHolder，里面装载不同的布局
        if (viewType == ITEM_TYPE_SECTION) {
            return new SectionHolder(inflater.inflate(R.layout.item_section, viewGroup, false));
        } else {
            return new NormalHolder(inflater.inflate(R.layout.item_normal, viewGroup, false));
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof NormalHolder) {//普通类型ViewHolder
            NormalHolder viewHolder = (NormalHolder) holder;
            viewHolder.getmTv_name().setText(mData.get(position).getName());
            viewHolder.getmIv_head().setText("$：" + position * 100);
        } else if (holder instanceof SectionHolder) {//特殊类型ViewHolder
            SectionHolder sectionHolder = (SectionHolder) holder;
            sectionHolder.getmIv_bg().setImageResource(R.mipmap.ic_launcher);
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        Goods goods = mData.get(position);
        return goods.getViewType();
    }
}
